package com.rykj.energy.database.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rykj.energy.common.dto.TaskCameraRelationDto;
import com.rykj.energy.common.enums.TaskState;
import com.rykj.energy.common.utils.CollectionUtils;
import com.rykj.energy.common.utils.StringUtils;
import com.rykj.energy.database.mapper.CameraObjectMapper;
import com.rykj.energy.database.mapper.CameraMapper;
import com.rykj.energy.database.mapper.TaskCameraRelationMapper;
import com.rykj.energy.database.mapper.TaskInfoMapper;
import com.rykj.energy.database.pojo.BasicCamera;
import com.rykj.energy.database.pojo.TaskCameraRelation;
import com.rykj.energy.database.pojo.TaskInfo;
import com.rykj.energy.database.service.ITaskInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("taskInfoService")
@Slf4j
public class TaskInfoServiceImpl implements ITaskInfoService {

    @Autowired
    private TaskInfoMapper taskInfoMapper;

    @Autowired
    private TaskCameraRelationMapper taskCameraRelationMapper;

    @Autowired
    private CameraObjectMapper cameraObjectMapper;

    @Autowired
    private CameraMapper cameraMapper;

    @Override
    @Transactional
    public TaskInfo saveOrUpdateTask(TaskInfo taskInfo) throws Exception {

        if(taskInfo!=null&&taskInfo.getId()!=null&&taskInfo.getId()!=0){

            taskInfo.setUpdateTime(new Date());
            taskInfoMapper.updateById(taskInfo);
        }else {
            taskInfo.setCreateTime(new Date());
            taskInfoMapper.insert(taskInfo);
        }
       // taskCameraRelationMapper.deleteByTaskId(taskInfo.getId());
        List<TaskCameraRelationDto> taskCameraRelations = taskCameraRelationMapper.findUnCloseByTaskId(taskInfo.getId(),null);
        List<TaskCameraRelationDto> taskCameras = taskInfo.getTaskCameras();
       //未传入的摄像头任务，清空参数
        taskCameraRelations.removeAll(taskCameras);
        if(CollectionUtils.isNotEmpty(taskCameraRelations)){
            List<Integer> delIds = new ArrayList<>();
            taskCameraRelations.forEach(r-> delIds.add(r.getId()));
            taskCameraRelationMapper.updateBusinessContent(delIds);
        }
        List<TaskCameraRelationDto> addTaskCameras = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(taskCameras)){
            for(TaskCameraRelationDto relation : taskCameras){
                cameraObjectMapper.deleteByTaskAndCameraId(relation.getTaskId(),relation.getCameraId(),relation.getPresetId());
                if(CollectionUtils.isNotEmpty(relation.getCameraObjectDtos())){
                    cameraObjectMapper.saveCamerasObject(relation.getCameraObjectDtos());
                }
                relation.setTaskCode(taskInfo.getTaskCode());
                relation.setTaskId(taskInfo.getId());
                if(StringUtils.isNotEmpty(relation.getId())&&relation.getId()>0){
                 //修改
                    TaskCameraRelation cameraRelation = taskCameraRelationMapper.selectById(relation.getId());
                    cameraRelation.setBusinessContent(relation.getBusinessContent());
                    if(StringUtils.isNotEmpty(relation.getExecPlan())){
                        cameraRelation.setExecPlan(relation.getExecPlan());
                    }
                    cameraRelation.setStatus(relation.getStatus());
                    cameraRelation.setUpdateTime(new Date());
                    taskCameraRelationMapper.updateById(cameraRelation);
                }else {
                   //新增
                    BasicCamera camera =cameraMapper.selectById(relation.getCameraId());
                    relation.setOrgId(camera.getOrgId());
                    relation.setCameraNo(camera.getCamNo());
                    relation.setCreateTime(new Date());
                    relation.setStatus(TaskState.close.getCode());
                    addTaskCameras.add(relation);
                }

            }

            System.out.println("CCCCCCCCCCCCCCCCCCCCCCCC:"+ JSON.toJSONString(taskCameras));
            if(CollectionUtils.isNotEmpty(addTaskCameras)){
                taskCameraRelationMapper.saveTaskCameras(addTaskCameras);
            }


        }
        return taskInfo;
    }

    @Override
    public TaskInfo findByCode(String taskCode) throws Exception {
        return taskInfoMapper.findInfoByCode(taskCode);
    }

    @Override
    public List<TaskCameraRelationDto> findRunTasks() throws Exception {
        return taskCameraRelationMapper.findTasksByStatus(TaskState.run.getCode());
    }

    @Override
    public TaskCameraRelation findByTaskIdAndCameraIdAndPreseId(Integer taskId, Integer cameraId, Integer preseId) {
        return taskCameraRelationMapper.findByTaskIdAndCameraIdAndPreseId(taskId,cameraId,preseId);
    }
}
